package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.service.IndxeService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private IndxeService indxeService;

    @GetMapping("/**")
    public String toIndex(Model model) {
        //加载一级分类
        List<CategoryEntity> categoryEntityList = indxeService.queryLvl1Categories();
        model.addAttribute("categories", categoryEntityList);

        return "index";
    }

    @GetMapping("index/cates/{pid}")
    @ResponseBody
    public ResponseVo<List<CategoryEntity>> queryLv123CatesByPid(@PathVariable("pid")Long pid){
        List<CategoryEntity> categoryEntities = indxeService.queryLv123CatesByPid(pid);

        return ResponseVo.ok(categoryEntities);
    }
}
